﻿using System;                                                       //System namespace
using System.Collections.Generic;
using System.Linq;
using System.Web;                                                   //web stuff
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;                                        //Database stuff

namespace UserStuff
{
    public partial class WebForm : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox textBox1;       //webcontrol name
        protected System.Web.UI.WebControls.TextBox textBox2;       //webcontrol locatioin
        protected System.Web.UI.WebControls.TextBox textBox3;       //webcontrol Instrument
        protected System.Web.UI.WebControls.TextBox textBox4;       //webcontol for searches
        protected System.Web.UI.WebControls.Label label1;           //webcontrol label

        //Connection string for database
        private string connectionString = "Data Source=mssql3.sqllive.co.uk;Integrated Security=False;"
               + "User ID=dataUser;Connect Timeout=15;Encrypt=False;Packet Size=4096;password=password1";

        //Things to do on page loading
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void button1Click(Object source, EventArgs e)
        {
            string sqlInsert = "INSERT INTO musicians VALUES('" + textBox1.Text + "', '" +
                textBox2.Text + "', '" + textBox3.Text + "');";
            
            using (SqlConnection dbconn = new SqlConnection(connectionString))
            {
                dbconn.Open();

                using (SqlCommand cmd = new SqlCommand(sqlInsert, dbconn)) 
                {
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (SqlException ax)
                    {
                        Console.WriteLine(ax);
                    }
                }
            }

            //clear the textfields after usage
            textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = "";
            label1.Text = "Thank you. Click View to see!!!";
        }

        //for searches
        protected void searchClick(Object source, EventArgs e)
        {

        }

        //button event for viewing database
        protected void button2Click(Object source, EventArgs e)
        {
            using (SqlConnection dbconn = new SqlConnection(connectionString))
            {
                dbconn.Open();                                                              

                using (SqlCommand sql = new SqlCommand("SELECT * FROM musicians;", dbconn))
                {
                    using (SqlDataReader reader = sql.ExecuteReader())
                    {
                        //this takes care of binding for the repeater
                        MusicianRepeater.DataSource = reader;       
                        MusicianRepeater.DataBind();
                   }
                }
            }
        }
    }
}